package exemplesLwpf;

import core.ObjectePersistentFctr;
import utilities.ConnectorSimple;
import utilities.MagatzemDades;
import utilities.Utilitats;

import java.io.IOException;
import java.io.Serializable;
import java.io.SerializablePermission;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 * User: Jordi
 * Date: 7/01/13
 * Time: 18:48
 * To change this template use File | Settings | File Templates.
 */
public class actorFactory extends ObjectePersistentFctr implements Serializable, Cloneable  {

    protected Properties sqlMap=null;

    public actorFactory(){
        sqlMap=carregaMappeig("actor.sql");
        initFactory();
        setRequestor(actor.class);
    }

    protected void initFactory(){
        try{
            ConnectorSimple cs=new ConnectorSimple();
            cs.setNombreDriver("com.mysql.jdbc.Driver");
            cs.setDbURL("jdbc:mysql://localhost:3306");
            cs.setNombreUsuario("root");
            cs.setPassword("lamara1608");
            this.utilitats = new Utilitats();
            utilitats.setDataSource(cs);
            setMapping(carregaMappeig("actor.properties"));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    public Properties carregaMappeig (String path){
        java.io.InputStream iStream=null;
        Properties propietats=new Properties();
        try{
            iStream =this.getClass().getResourceAsStream(path);
            propietats.load(iStream);
        }
        catch (IOException ioExcep){
            throw new RuntimeException(ioExcep);
        }
        finally {
            try {
                iStream.close();
            }
            catch (IOException ioExcep){
                throw new RuntimeException(ioExcep);
            }
        }
        return propietats;
    }

    public void select(){
        System.out.println("\n======================= Ejemplo de uso de varios metodos para rellenar collections                ===========================\n");
        try{
            setMapping(carregaMappeig("actor.properties"));
            String sql = sqlMap.getProperty("buscarTotsActors");
            System.out.println("============== como lista ValoresObjeto (primera columna valor, segunda contenido)                    ==================");
            List list = utilitats.getResultadoComoValoresListaObjetos(sql);
            System.out.println(list);
            System.out.println("============== como Vector de Vectores ==================");
            Vector vector1 = utilitats.getResultadoComoVector(sql);
            System.out.println(vector1);
            System.out.println("============== como Lista de tipo Map (key=nombre columna, valor=lista valores por columna)                    ==================");
            Map map = utilitats.getResultadoComoMap(sql);
            System.out.println(map);
            System.out.println("============== como array de ContenedorDatos ==================");
            MagatzemDades[] cd = utilitats.getResultadoComoArrayMagatzemDades(sql, false);
            for (int i = 0;i < cd.length;i++){
                System.out.println(cd[i]);
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            try{
                utilitats.release(true);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
}
